import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import org.jbox2d.collision.CircleDef;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;

import pulpfizz.physics.Actor;
import pulpfizz.physics.CollisionUtils;
import pulpfizz.physics.Material;
import pulpfizz.pulp.body.BodyDrawer;
import pulpfizz.pulp.body.Java2DBodySprite;
import pulpfizz.pulp.body.PhysicsLayer;


public class Bullet extends Actor implements BodyDrawer
{
	Java2DBodySprite sprite;
	
	public static final int SHOOT_GRAB_DELAY = 50;
	
	static int count=0;
	
	Body body;
	public long validGrabTime;
	Player parent;

	public float size;
	public float radius;
	
	public static final int CATEGORY = 0x4;
	public static final float DENSITY_MULT = .75f;
	
	public static float getRadForSize(float size)
	{
		return (float) Math.sqrt(size/Math.PI);
	}
	
	public Bullet(PhysicsLayer physics, float size, Player parent)
	{
		this.size = size;
		this.parent = parent;
		CircleDef cd = new CircleDef();
		radius = getRadForSize(size);
		cd.radius = radius;
		
		BodyDef bd = new BodyDef();
		bd.isBullet = true;
		body = physics.getWorld().createBody(bd);
		body.createShape(cd);
		
		Material m = Material.WOOD();
		m.setDensity(m.getDensity()*DENSITY_MULT);
		m.applyAll(body);
		
		sprite = new Java2DBodySprite(body,physics,1);
		sprite.setPadding(2);
		sprite.setBodyDrawer(this);
		
		// Create the actor.
		setName("Bullet" + (++count));
		addToGroup(TwoPlayerScene.BULLET_GROUP);
		addBody(body);
		
		validGrabTime = System.currentTimeMillis() + SHOOT_GRAB_DELAY;
		
		physics.add(sprite);
		
		CollisionUtils.setCategory(Bullet.CATEGORY, body);
		CollisionUtils.maskOut(Player.CATEGORY,body);
	}
	
	Ellipse2D.Float ellipse = new Ellipse2D.Float();
	public void drawBody(PhysicsLayer physics, Body b, Graphics2D g2)
	{
		g2.setColor(new Color(200,255,200));
		float one = 1/physics.getScaling();
		g2.setStroke(new BasicStroke(one*2));
		ellipse.setFrame(-radius+one, -radius+one, radius*2-one*2, radius*2-one*2);
		g2.draw(ellipse);
	}

	public boolean needsRedraw()
	{
		return false;
	}
	
}
